Fix #951 — clean up Monitor_LongQueries_*.trc files in data_retention#953
Merged
Conversation
config.data_retention only purged rows from collect.* tables, leaving
the .trc files written by collect.trace_management_collector to
accumulate forever in the SQL Server error log directory. Reporter saw
~7 GB / 106 files going back 2 months on a busy server.
Adds a trace file cleanup block to config.data_retention that:
- reads retention_days from config.collection_schedule for
trace_management_collector (30-day fallback), honoring the same
@retention_days override semantics as the table cleanup above it
- derives the error log directory via SERVERPROPERTY('ErrorLogFileName')
the same way collect.trace_management_collector does
- pre-checks via xp_dirtree to skip xp_delete_file when no matching
files exist, suppressing the "Msg 22049" noise xp_delete_file prints
on empty matches
- calls master.dbo.xp_delete_file to delete files older than the cutoff
(active trace file stays locked open, so it survives)
- TRY/CATCH treats Msg 22049 as benign (no files needed cleanup) on
older SQL versions where it surfaces as a catchable error
- logs a separate config.collection_log row so trace cleanup is auditable
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
config.data_retentiononly purged rows fromcollect.*tables; the.trcfiles written bycollect.trace_management_collectorwere never deleted and accumulated forever in the SQL Server error log directory (reporter saw ~7 GB / 106 files going back 2 months).config.data_retentionthat readsretention_daysfromconfig.collection_schedulefortrace_management_collector(30-day fallback), derives the error log directory viaSERVERPROPERTY('ErrorLogFileName')(same logic the collector uses), and callsmaster.dbo.xp_delete_fileto deleteMonitor_LongQueries_*.trcfiles older than the cutoff. The active trace file stays locked open so it survives.xp_dirtree(cross-version, SQL 2016+) to skipxp_delete_filewhen no matching files exist — suppresses the noisy "Msg 22049" outputxp_delete_fileprints on empty matches.TRY/CATCHalso carves out 22049 as benign on older SQL versions where it surfaces as a catchable error.@retention_daysoverride semantics:NULLreads fromconfig.collection_schedule, non-NULLuses the flat value (matches table-cleanup behavior directly above in the proc).config.collection_logrow so trace cleanup is independently auditable from table cleanup.Test plan
sqlcmd -i install/43_data_retention.sqlon SQL 2022EXECUTE config.data_retention @debug = 1runs end-to-end without aborting, logs SUCCESS for trace cleanup with pattern + cutoff visiblexp_dirtreecorrectly counts matchingMonitor_LongQueries_*.trcfiles.trcfiles) that old files are actually deletedsys.tracesshows it running)🤖 Generated with Claude Code